package com.xingkeduo.remote;

import com.xingkeduo.dto.ShopsDTO;
import com.xingkeduo.dto.StoreRemoteDto;
import com.xingkeduo.remote.feign.StoreFeignService;
import com.xingkeduo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author chi  2019-03-13 10:43
 **/
@Service
@Slf4j
public class StoreRemoteService {



    @Resource
    private StoreFeignService storeFeignService;

    @Cacheable(value = "cache:comments:getByStoreId", key = "#storeId")
    public StoreRemoteDto getByStoreId(String storeId){
        log.info("getByStoreId:{}", storeId);
        try {
        ResponseUtils<ShopsDTO> responseUtils = storeFeignService.doGet(Long.valueOf(storeId));
        if(responseUtils.success()){
            ShopsDTO shopsDTO= responseUtils.getData();
            return StoreRemoteDto.builder()
                    .storeId(Math.toIntExact(shopsDTO.getId()))
                    .storeName(shopsDTO.getName())
                    .cityId(String.valueOf(shopsDTO.getCitiesId()))
                    .cityName(shopsDTO.getCitiesName())
                    .branchOfficeId(String.valueOf(shopsDTO.getOfficeId()))
                    .branchOfficeName(shopsDTO.getOfficeName())
                    .vendorId(String.valueOf(shopsDTO.getVendorId()))
                    .vendorName(shopsDTO.getVendorName())
                    .storeAddress(shopsDTO.getAddress())
                    .daysList(shopsDTO.getDaysList())
                    .build();
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
